import time
import sys
import json
import io
# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')
# 创建一个新的输出流，并设置编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 将 当且路径 添加到path中
sys.path.append(r'.')
from DrissionPage import ChromiumPage


def load_param_datas_from_file(file_path):
    with open(file_path, "r", encoding='utf-8') as f:
        json_file_datas = json.loads(f.read())    # load的传入参数为字符串类型
        return json_file_datas

def down_video():
    json_file_datas = load_param_datas_from_file('down_video.data.json')
    port = json_file_datas['port']
    url = json_file_datas['url']
    saveFile = json_file_datas['saveFile']
    fileName = json_file_datas['fileName']
    page = ChromiumPage(addr_or_opts= '127.0.0.1:' + str(port))
    tab = page.new_tab()  # 新建标签页，获取标签页对象
    tab.get(url)

    time.sleep(2)
    mp4_real_url = tab.url
    page.download(mp4_real_url, saveFile, fileName)

    # 关闭窗口
    tab.close()

down_video()



